package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.GeoRegion;
import com.tbit.uqbike.object.pojo.GeoRegionBound;
import com.tbit.uqbike.service.business.GeoRegionBoundService;
import com.tbit.uqbike.service.business.GeoRegionService;
import com.tbit.uqbike.webmanager.dao.core.GeoRegionBoundDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 运维分区绑定(GeoRegionBound)表服务实现类
 *
 * @author jing
 * @since 2020-11-24 09:43:39
 */
@Service("geoRegionBoundService")
public class GeoRegionBoundServiceImpl implements GeoRegionBoundService {
    @Resource
    private GeoRegionBoundDao geoRegionBoundDao;
    @Resource
    private GeoRegionService geoRegionService;

    /**
     * 通过ID查询单条数据
     *
     * @param regionId 主键
     * @return 实例对象
     */
    @Override
    public List<GeoRegionBound> queryById(Integer regionId) {
        return this.geoRegionBoundDao.queryById(regionId);
    }

    @Override
    public List<GeoRegionBound> queryByIds(List<Integer> regionId) {
        return geoRegionBoundDao.queryByIds(regionId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<GeoRegionBound> queryAllByLimit(int offset, int limit) {
        return this.geoRegionBoundDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param geoRegionBound 实例对象
     * @return 实例对象
     */
    @Override
    public GeoRegionBound insert(GeoRegionBound geoRegionBound) {
        GeoRegion geoRegion = geoRegionService.queryById(geoRegionBound.getRegionId());
//        /**权限判断*/
//        for (Role role : info.getRoles()) {
//            if (!role.getRoleSort().equals("0") && !role.getRoleSort().equals("1") &&
//                    !role.getRoleSort().equals("1") && !role.getRoleSort().equals("3")  ){
//                throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
//            }
//        }

        /**判断是否存在分区*/
        if (null == geoRegion) {
            throw new BaseException(SystemCodeEnum.GEOREGION_NOT_EXIST);
        }
        /**
         * 判断该用户是否添加
         * */
        GeoRegionBound geoRegionBound1 = new GeoRegionBound(geoRegionBound.getAccountId(), geoRegionBound.getRegionId(), geoRegionBound.getAccountUserId());
        List<GeoRegionBound> geoRegionBounds = geoRegionBoundDao.queryAll(geoRegionBound1);
        if (0 < geoRegionBounds.size()) {
            throw new BaseException(SystemCodeEnum.USER_HAVE_EXIST);
        }
        if (geoRegionBound.getIsLeader() == 1) {
            geoRegionBound1 = new GeoRegionBound(geoRegionBound.getRegionId(), geoRegionBound.getIsLeader());
            geoRegionBounds = geoRegionBoundDao.queryAll(geoRegionBound1);
            if (0 < geoRegionBounds.size()) {
                throw new BaseException(SystemCodeEnum.ONLY_ONE_LEADER);
            }
        }


        geoRegionBound.setBoundTime(new Date());
        this.geoRegionBoundDao.insert(geoRegionBound);
        return geoRegionBound;
    }

    /**
     * 修改数据
     *
     * @param geoRegionBound 实例对象
     * @return 实例对象
     */
    @Override
    public void update(GeoRegionBound geoRegionBound) {
        this.geoRegionBoundDao.update(geoRegionBound);
    }

    /**
     * 通过主键删除数据
     *
     * @param regionId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer regionId, Integer accountUserId) {
        return this.geoRegionBoundDao.deleteById(regionId, accountUserId) > 0;
    }
}